package org.jeecg.modules.product.until;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class HolidayFetcherUtils {
    public static List<Map<String,String>> fetchHolidayByYear(String apiKey,String yearUrl,String year) {
        List<Map<String,String>> holidayList = new ArrayList<>();
        try {
            //String apiKey = "2577592d8d28673d6bbc5ac6ccf118c8";
            //String apiUrl = "http://apis.juhe.cn/fapig/calendar/day";
            //String apiUrl = "http://v.juhe.cn/calendar/month";
            //String yearUrl = "http://v.juhe.cn/calendar/year";


            HashMap<String, String> map = new HashMap<>();
            map.put("key", apiKey);
            map.put("year", "2025");

            //2.创建URL对象
            URL url = new URL(String.format("%s?%s", yearUrl, params(map)));
            //3.创建HttpURLConnection对象
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");

            //4.获取响应码
            if (conn.getResponseCode() == 200) {
                //5.获取响应内容
                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                StringBuffer repsonse = new StringBuffer();
                String line;
                while ((line = reader.readLine()) != null) {
                    repsonse.append(line);
                }
                reader.close();

                //6.输出响应内容
                if (repsonse != null && repsonse.length() > 0) {
                    Map<String, Object> mapRepsonse = JSON.parseObject(repsonse.toString(), Map.class);
                    if (mapRepsonse.get("reason").equals("Success")) {
                        Map<String, Object> mapResult = (Map<String, Object>) mapRepsonse.get("result");
                        Map<String, Object> maoData = (Map<String, Object>) mapResult.get("data");
                        holidayList = (List<Map<String,String>>) maoData.get("holiday_list");
                    }else {
                        log.info("返回结果错误!");
                    }
                }
            }else {
                log.info("请求失败,响应码:{}"+conn.getResponseCode());
            }
            //7.关闭连接
            conn.disconnect();
        }catch (Exception e){
            e.printStackTrace();
        }
        log.info("==========holidayList============"+holidayList);
        return holidayList;
    }

    public static void main(String[] args) {
        fetchHolidayByYear("2577592d8d28673d6bbc5ac6ccf118c8","http://v.juhe.cn/calendar/year","2025");
    }

//    public static void main(String[] args) {
//        try {
//            //1.定义API地址
//            String apiKey = "29ae506068ba87e2462923daf11fa0fb";
//            String apiUrl = "http://apis.juhe.cn/fapig/calendar/day";
//            //String apiUrl = "http://v.juhe.cn/calendar/month";
//            //String apiUrl = "http://v.juhe.cn/calendar/year";
//
//
//            HashMap<String, String> map = new HashMap<>();
//            map.put("key", apiKey);
//            map.put("date", "2025-02-24");
//            map.put("detail", "");
//
//            //2.创建URL对象
//            URL url = new URL(String.format("%s?%s", apiUrl, params(map)));
//            //3.创建HttpURLConnection对象
//            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//            conn.setRequestMethod("GET");
//
//            //4.获取响应码
//            if (conn.getResponseCode() == 200) {
//                //5.获取响应内容
//                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//                StringBuffer repsonse = new StringBuffer();
//                String line;
//                while ((line = reader.readLine()) != null) {
//                    repsonse.append(line);
//                }
//                reader.close();
//
//                //6.输出响应内容
//                log.info("===========请求结果是==============="+repsonse.toString());
//            }else {
//                log.info("请求失败,响应码:{}"+conn.getResponseCode());
//            }
//            //7.关闭连接
//            conn.disconnect();
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }

    public static String params(Map<String, String> map) {
        return map.entrySet().stream()
                .map(entry -> {
                    try {
                        return entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.toString());
                    } catch (Exception e) {
                        e.printStackTrace();
                        return entry.getKey() + "=" + entry.getValue();
                    }
                })
                .collect(Collectors.joining("&"));
    }
}
